package com.push;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.constant.Constant;
import com.tencent.android.tpush.XGPushBaseReceiver;
import com.tencent.android.tpush.XGPushClickedResult;
import com.tencent.android.tpush.XGPushRegisterResult;
import com.tencent.android.tpush.XGPushShowedResult;
import com.tencent.android.tpush.XGPushTextMessage;

public class XGPushReceiver extends XGPushBaseReceiver {

	@Override
	public void onDeleteTagResult(Context context, int arg1, String arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onNotifactionClickedResult(Context context,
			XGPushClickedResult arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onNotifactionShowedResult(Context context,
			XGPushShowedResult arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onRegisterResult(Context context, int error_code,
			XGPushRegisterResult arg2) {
		// TODO Auto-generated method stub
		Log.d("cyd", "error_code" + error_code);
		// if (error_code == 0) {
		// context.getSharedPreferences("app", Context.MODE_PRIVATE).edit()
		// .putBoolean("regist", true).commit();
		// }

	}

	@Override
	public void onSetTagResult(Context context, int arg1, String arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTextMessage(Context context, XGPushTextMessage arg1) {
		// TODO Auto-generated method stub
		String reason = arg1.getContent();
		Intent it = new Intent();
		Log.d("cyd", reason);
		it.putExtra("title", arg1.getTitle());
		String s = arg1.getCustomContent();
		Log.d("cyd", "custom_content" + s);
		try {
			JSONObject obj = new JSONObject(s);
			String msgType = obj.getString("msg_type");
			String action = obj.getString("action");
			if (msgType.equals("way_bill")) {
				it.setAction(Constant.ACTION_WAYBILL);
				if (action.equals("destroy") && reason != null)
					it.putExtra("reason", reason);
			}
			if (msgType.equals("transportation_info")) {
				it.setAction(Constant.ACTION_TRANSPORT);
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		context.sendBroadcast(it);

	}

	@Override
	public void onUnregisterResult(Context context, int arg1) {
		// TODO Auto-generated method stub

	}

}
